import {View} from "@tarojs/components";
import React, {useEffect, useState} from "react";
import {NoticeModel} from "@/common/apis/model";
import {getNoticeList} from "@/common/apis/home_datasource";
import LogUtils from "@/utils/log";
import {Cell, Empty} from "@antmjs/vantui";
import Utils from "@/utils/common";
import Taro, {usePullDownRefresh, useReachBottom} from "@tarojs/taro";

definePageConfig({
  navigationBarTitleText: '通知列表',
  enablePullDownRefresh: true,
  onReachBottomDistance: 50,
})
export default function Page1() {
  const [items, setItems] = useState<NoticeModel[]>()

  useEffect(() => {
    getNoticeList().then((res) => {
      setItems(res)
    })
    return () => {
      LogUtils.info("Page1", "Page1")
    }
  }, []);

  usePullDownRefresh(()=>{
    return new Promise((resolve) => {
      setTimeout(() => {
        Utils.toast('刷新成功')
        Taro.stopPullDownRefresh();
        resolve(true)
      }, 1000)
    })
  })

  useReachBottom(() => {
    return new Promise((resolve) => {
      setTimeout(() => {
        Utils.toast('useReachBottom')
        resolve(true)
      }, 1000)
    })
  })

  return (
    <View style={{
      display: 'flex',
      flexDirection: 'column',
      height: '100VH',
      paddingBottom: '50px',
      boxSizing: 'border-box',
      backgroundColor: '#f2f2f2',
    }}>
      {items && !Utils.isEmpty(items) ? items.map((e, i) => {
        return <Cell title={e.title} key={i}  onClick={() => {}} label={`${e.source}  ${e.time}`}  />
      }) : <Empty description='暂无数据' />}

    </View>
  )
}


